package com.sm.service.Impl;

import com.sm.dao.*;
import com.sm.entity.*;
import com.sm.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userRoleService")
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    UserRoleDao userRoleDao;
    @Autowired
    WagesDao wagesDao;
    @Autowired
    AttendanceDao attendanceDao;
    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    public UserInfo findByUid(Integer uid) throws Exception {
        return userDao.findByUid(uid);
    }

    public List<UserInfo> findAllUserByStatus() throws Exception {
        return userDao.findAllUser();
    }

    public UserInfo findByUsername(String username) throws Exception {
        return userDao.findByUsername(username);
    }

    public List<Role> findAllRole() throws Exception {
        return roleDao.findAllRole();
    }

    public List<Role> findUserRole(Integer uid) throws Exception {
        return roleDao.findRoleByUid(uid);
    }

    public void addUser(UserInfo userInfo,Integer[] admin) throws Exception {
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        userInfo.setStatus(3);
        userDao.addUser(userInfo);
        userInfo = userDao.findByUsername(userInfo.getUsername());
        Integer uid = userInfo.getUid();
        for (Integer rid:admin){
            if(rid == 4){//如果员工是管理员等级直接为正式员工
                userDao.updateUserStatus(uid,1);
            }
            userRoleDao.addUserRole(uid,rid);
        }
        //添加新员工时创建三条工资表记录 三条考勤总表记录
        for(int i=0;i<3;i++){
            wagesDao.addWages(uid,i);
            attendanceDao.addAttendanceTotal(uid,i);
        }
    }

    public void updateUser(UserInfo userInfo) throws Exception {
        userDao.updateUser(userInfo);
    }

    public void updatePassword(UserInfo userInfo) throws Exception {
        userDao.updateUser(userInfo);
    }

    public void updateUserStatus(Integer uid, Integer[] rids) throws Exception {
        userRoleDao.deletRoleByUid(uid);
        for (Integer rid:rids){
            userRoleDao.addUserRole(uid,rid);
        }
    }

    public void deleteUser(Integer uid) throws Exception {
        UserInfo userInfo = new UserInfo();
        userInfo.setUid(uid);
        userDao.deleteUser(userInfo);
    }

    //用于登录时根据姓名查询员工 封装成userDetail对象
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserInfo userInfo = null;
        try {
            userInfo = userDao.findByUsername(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        UserLogin user = new UserLogin(userInfo.getUid(),userInfo.getName(),userInfo.getUsername(),userInfo.getPassword(),
                (userInfo.getStatus()==0?false:true),
                true,true,true,getAuthority(userInfo.getRoles()));
        return user;
    }

    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles){
        List<SimpleGrantedAuthority> list = new ArrayList<SimpleGrantedAuthority>();
        for(Role role : roles){
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }
        return list;
    }

    public List<UserInfo> searchUser(String name) throws Exception {
        return userDao.findUserLikeName(name,null);
    }

    public UserInfo searchUserByUid(Integer uid) throws Exception {
        return userDao.findByUid(uid);
    }
}
